package com.itheima.xx.service;

import com.itheima.xx.dao.UserMapper;
import com.itheima.xx.pojo.User;
import com.itheima.xx.util.SqlSessionUtil;
import org.apache.ibatis.session.SqlSession;

/**
 * Description:
 *
 * @author codermao
 * @date 2023/11/1 14:24
 */
public class UserServiceImpl {

    public boolean register(User user) {
        SqlSession sqlSession = null;
        try{
             sqlSession = SqlSessionUtil.getSession(false);
            UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
            User u  = userMapper.queryUserByUserName(user.getUserName());
            if (u == null){
                userMapper.regist(user);
                sqlSession.commit();
                return true;
            }else{
                return false;
            }
        }finally {
            if (sqlSession != null){
                sqlSession.close();
            }
        }
    }

    public User login(User user) {
        // 2.使用mybatis工具类获取session对象
        SqlSession sqlSession = SqlSessionUtil.getSession(false);
        // 3.使用session对象调用方法获取接口Mapper对象
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        // 4.使用Mapper对象调用接口的登录方法
        User u = mapper.login(user);
        // 5.释放资源
        sqlSession.close();
        // 6.返回给web层对象u
        return u;
    }
}
